package wiki.zimo.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @auther: 子墨
 * @datetime: 2018/10/30 12:37
 * @describe: 随机验证码工具类
 */
public class VerifyCodeUtils {

    // 验证图片宽、高
    private int w = 70;
    private int h = 35;
    private Random r = new Random();

    // 字体样式
    private String[] fantFamily = {"宋体", "华文新魏", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};
    // 随机字符的取值范围
    private String codes = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    // 验证码图片背景颜色
    private Color bgColor = new Color(255, 255, 255);
    // 生成的随机验证码内容
    private String content;

    /**
     * @auther: 子墨
     * @date: 2018/10/30 12:32
     * @describe: 随机生成字体颜色
     * @param: []
     * @return: java.awt.Color
     * @version v1.0
     */
    private Color randomColor() {
        int red = r.nextInt(150);
        int green = r.nextInt(150);
        int blue = r.nextInt(150);
        return new Color(red, green, blue);
    }

    /**
     * @auther: 子墨
     * @date: 2018/10/30 12:32
     * @describe: 随机生成字体样式
     * @param: []
     * @return: java.awt.Font
     * @version v1.0
     */
    private Font randomFontFamily() {
        int index = r.nextInt(fantFamily.length);
        String fontName = fantFamily[index];
        int style = r.nextInt(4);// 生成随机的样式 0（无样式） 1(粗体) 2（斜体） 3 (粗体、斜体)
        int size = r.nextInt(5) + 24;// 生成随机字号 ，24-28
        return new Font(fontName, style, size);

    }

    /**
     * @auther: 子墨
     * @date: 2018/10/30 12:32
     * @describe: 绘制干扰线
     * @param:
     * @return:
     * @version v1.0
     */
    private void drawLine(BufferedImage image) {
        // 绘制三条
        int num = 3;
        Graphics2D g = (Graphics2D) image.getGraphics();
        // 随机生成坐标，生成两个点的坐标
        for (int i = 0; i < num; i++) {
            int x1 = r.nextInt(w);
            int y1 = r.nextInt(h);
            int x2 = r.nextInt(w);
            int y2 = r.nextInt(h);
            g.setStroke(new BasicStroke(1.5F));
            g.setColor(Color.BLUE);
            g.drawLine(x1, y1, x2, y2);
        }
    }

    /**
     * @auther: 子墨
     * @date: 2018/10/30 12:32
     * @describe: 随机生成一个字符
     * @param: []
     * @return: char
     * @version v1.0
     */
    private char randomChar() {
        int index = r.nextInt(codes.length());
        return codes.charAt(index);
    }

    /**
     * @auther: 子墨
     * @date: 2018/10/30 12:31
     * @describe: 创建背景板为白色，高宽确定的绘制环境
     * @param: []
     * @return: java.awt.image.BufferedImage
     * @version v1.0
     */
    private BufferedImage createImage() {
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setColor(this.bgColor);
        g.fillRect(0, 0, w, h);
        return image;
    }

    /**
     * @auther: 子墨
     * @date: 2018/10/30 12:31
     * @describe: 得到随机验证码的图片
     * @param: []
     * @return: java.awt.image.BufferedImage
     * @version v1.0
     */
    public BufferedImage getImage() {
        BufferedImage image = createImage();
        Graphics2D g = (Graphics2D) image.getGraphics();
        StringBuilder sb = new StringBuilder();

        // 向图中画4个字符
        for (int i = 0; i < 4; i++) {
            String s = randomChar() + "";
            sb.append(s);

            float x = i * 1.0F * w / 4;
            g.setFont(randomFontFamily());
            g.setColor(randomColor());
            g.drawString(s, x, h - 5);
        }
        this.content = sb.toString();
        drawLine(image);
        return image;
    }

    public String getContent() {
        return content;
    }

}
